﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IllustratedCsharp7_ch15_PassData
{
    //自定义类
    public class IncrementerEventArgs : EventArgs //自定义类派生自EventArgs
    { 
        public int IterationCount { get; set;}  //存储一个整数
    }
    class Incrementer
    {
        //使用自定义类的泛型委托
        public event EventHandler<IncrementerEventArgs> CountedADozen;
        public void DoCount()
        {
            IncrementerEventArgs args = new IncrementerEventArgs();
            for (int i = 1; i < 100; i++)
                if (i % 12 == 0 && CountedADozen != null)
                {
                    args.IterationCount = i;
                    CountedADozen(this,args);
                }    
        }
    }
    class Dozens
    { 
        public int DozensCount { get; private set; }
        public Dozens(Incrementer incrementer)
        {
            DozensCount = 0;
            incrementer.CountedADozen += IncrementDozensCount;
        }

        void IncrementDozensCount(object source, IncrementerEventArgs e)
        {
            Console.WriteLine($"Incremented at iteration:{e.IterationCount} in {source.ToString()}");
            DozensCount++;
        }
    }
    class Chapt15_6_1PassDataByExtendingEventArgs
    {
        static void _Main()
        {
            Incrementer incrementer = new Incrementer();
            Dozens dozensCounter = new Dozens(incrementer);

            incrementer.DoCount();
            Console.WriteLine($"Number of dozens = {dozensCounter.DozensCount}");
        }
    }
}
